package com.todo.event.activity.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.event.activity.common.enums.EventDecisionStatusEnum;
import com.todo.event.activity.infrastructure.entity.condition.EventDecisionCondition;
import com.todo.event.activity.infrastructure.entity.po.EventDecisionPO;
import com.todo.event.activity.infrastructure.mapper.EventDecisionMapper;
import com.todo.event.activity.infrastructure.repository.EventDecisionRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/7 09:26
 * @description
 */
@Slf4j
@Repository
public class EventDecisionRepositoryImpl implements EventDecisionRepository {

    @Resource
    private EventDecisionMapper eventDecisionMapper;

    @Override
    public Boolean insertEventDecisionPO(EventDecisionPO eventDecisionPO) {
        if (eventDecisionPO == null) {
            return false;
        }
        if (eventDecisionPO.getName() == null || eventDecisionPO.getDecisionType() == null || eventDecisionPO.getFirstEvent() == null) {
            return false;
        }
        return eventDecisionMapper.insertEventDecisionPO(eventDecisionPO) > 0;
    }

    @Override
    public Boolean updateEventDecisionPOById(EventDecisionPO eventDecisionPO) {
        if (eventDecisionPO == null || eventDecisionPO.getId() == null) {
            return false;
        }
        return eventDecisionMapper.updateEventDecisionPOById(eventDecisionPO) > 0;
    }

    @Override
    public Boolean removeEventDecisionPOById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return eventDecisionMapper.removeEventDecisionPOById(id) > 0;
    }

    @Override
    public EventDecisionPO queryEventDecisionPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return eventDecisionMapper.queryEventDecisionPOById(id);
    }

    @Override
    public PageResult<EventDecisionPO> queryEventDecisionByCondition(EventDecisionCondition eventDecisionCondition) {
        PageResult<EventDecisionPO> pageResult = new PageResult<>();
        if (eventDecisionCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(eventDecisionCondition.getPageIndex());
        pageResult.setPageSize(eventDecisionCondition.getPageSize());
        long count = eventDecisionMapper.queryCountByCondition(eventDecisionCondition);
        if (count <= 0) {
            log.info("EventDecisionRepository#queryEventDecisionByCondition 活动数据查询完成，总数为空 eventDecisionCondition：【{}】", JSONObject.toJSONString(eventDecisionCondition));
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, eventDecisionCondition.getPageSize()));
        pageResult.setTotal(count);
        List<EventDecisionPO> eventDecisionPOList = eventDecisionMapper.queryEventDecisionListByCondition(eventDecisionCondition);
        if (CollectionUtils.isEmpty(eventDecisionPOList)) {
            pageResult.setHasNext(false);
            log.info("EventDecisionRepository#queryEventDecisionByCondition 活动数据查询完成，结果为空 eventDecisionCondition：【{}】", JSONObject.toJSONString(eventDecisionCondition));
            return pageResult;
        }
        pageResult.setResults(eventDecisionPOList);
        pageResult.setCurrentIndex(eventDecisionPOList.get(0).getId());
        pageResult.setHasNext(eventDecisionPOList.size() >= eventDecisionCondition.getPageSize());
        return pageResult;
    }

    @Override
    public EventDecisionPO queryEventDecisionByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return eventDecisionMapper.queryEventDecisionByName(name);
    }

    @Override
    public Boolean updateEventDecisionStatus(Long id, EventDecisionStatusEnum oldStatus, EventDecisionStatusEnum newStatus) {
        if (id == null || oldStatus == null || newStatus == null) {
            return false;
        }
        return eventDecisionMapper.updateEventDecisionStatus(id, oldStatus.getType(), newStatus.getType()) > 0;
    }

    @Override
    public List<EventDecisionPO> queryEventDecisionByEventCode(String eventCode) {
        if (StringUtils.isBlank(eventCode)) {
            return Collections.emptyList();
        }
        List<EventDecisionPO> result = new ArrayList<>();
        List<EventDecisionPO> first = eventDecisionMapper.queryFirstDecisionByEventCode(eventCode, EventDecisionStatusEnum.RUNNING.getType());
        List<EventDecisionPO> second = eventDecisionMapper.querySecondDecisionByEventCode(eventCode, EventDecisionStatusEnum.RUNNING.getType());
        result.addAll(first);
        result.addAll(second);
        return result;
    }

}
